/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2022并保留所有权利。
 * Copyright 2022, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.sync.hronized.waitnotiy1;

import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class MyTest {

    int num = 1; //对num进行逐1累加
    final ReentrantLock lock = new ReentrantLock();

    final class MyWork implements Runnable {
        public void run() {
            while (true) {
                try {
                    Thread.sleep(new Random().nextInt(2000)+300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName()+" : "+ ++num);
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    public void execute() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.submit(new MyWork());
        executorService.submit(new MyWork());
        executorService.submit(new MyWork());
    }
}